package sps.admin.module.business.workinghours.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import sps.admin.constant.AdminSwaggerTagConst;
import sps.admin.module.business.workinghours.domain.form.WorkingHoursAddForm;
import sps.admin.module.business.workinghours.domain.form.WorkingHoursQueryForm;
import sps.admin.module.business.workinghours.domain.form.WorkingHoursUpdateForm;
import sps.admin.module.business.workinghours.domain.form.WorkingOvertimeAnalysisForm;
import sps.admin.module.business.workinghours.domain.vo.*;
import sps.admin.module.business.workinghours.service.WorkingHoursService;
import sps.admin.util.AdminRequestUtil;
import sps.base.common.domain.ValidateList;
import sps.base.common.domain.ResponseDTO;
import sps.base.common.domain.PageResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import sps.base.common.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 工时功能 Controller
 *
 * @Author yjg
 * @Date 2024-09-09 11:02:13
 * @Copyright v1.0
 */

@RestController
@Tag(name = AdminSwaggerTagConst.Business.WORKING_HOURS)
public class WorkingHoursController {

    @Resource
    private WorkingHoursService workingHoursService;


    @Operation(summary = "分页查询 @author yjg")
    @PostMapping("/workingHours/queryPage")
    @SaCheckPermission("workinghours:query")
    public ResponseDTO<PageResult<WorkingHoursVO>> queryPage(@RequestBody @Valid WorkingHoursQueryForm queryForm) {
        queryForm.setCreateUserId(SmartRequestUtil.getRequestUserId());
        return ResponseDTO.ok(workingHoursService.queryPage(queryForm));
    }

    @Operation(summary = "工时审批分页查询 @author yjg")
    @PostMapping("/workingHoursAllow/queryPage")
    @SaCheckPermission("workinghoursallow:query")
    public ResponseDTO<PageResult<WorkingHoursVO>> queryAllowPage(@RequestBody @Valid WorkingHoursQueryForm queryForm) {
        return ResponseDTO.ok(workingHoursService.queryAllowPage(queryForm));
    }

    @Operation(summary = "加班审批分页查询 @author yjg")
    @PostMapping("/workingOverTimeAllow/queryPage")
    @SaCheckPermission("workingovertimeallow:query")
    public ResponseDTO<PageResult<WorkingHoursVO>> queryWorkingOvertimeAllowPage(@RequestBody @Valid WorkingHoursQueryForm queryForm) {
        return ResponseDTO.ok(workingHoursService.queryAllowPage(queryForm));
    }

    @Operation(summary = "添加 @author yjg")
    @PostMapping("/workingHours/add")
    @SaCheckPermission("workinghours:add")
    public ResponseDTO<String> add(@RequestBody @Valid WorkingHoursAddForm addForm) {
        addForm.setCreateUserId(SmartRequestUtil.getRequestUserId());
        return workingHoursService.add(addForm);
    }

    @Operation(summary = "更新 @author yjg")
    @PostMapping("/workingHours/update")
    @SaCheckPermission("workinghours:update")
    public ResponseDTO<String> update(@RequestBody @Valid WorkingHoursUpdateForm updateForm) {
        return workingHoursService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author yjg")
    @PostMapping("/workingHours/batchDelete")
    @SaCheckPermission("workinghours:delete")
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Integer> idList) {
        return workingHoursService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author yjg")
    @GetMapping("/workingHours/delete/{workingHoursId}")
    @SaCheckPermission("workinghours:batchDelete")
    public ResponseDTO<String> batchDelete(@PathVariable Integer workingHoursId) {
        return workingHoursService.delete(workingHoursId);
    }

    @Operation(summary = "审批 @author yjg")
    @GetMapping("/workingHours/allow/{workingHoursId}/{flag}")
    @SaCheckPermission("workinghours:allow")
    public ResponseDTO<String> allow(@PathVariable Long workingHoursId,@PathVariable Integer flag) {
        WorkingHoursUpdateForm form = new WorkingHoursUpdateForm();
        form.setWorkingHoursId(workingHoursId);
        form.setAllowFlag(flag);
        form.setAllowTime(SmartLocalDateUtil.toLocalDateTime(new Date()));
        form.setAllowMember( SmartRequestUtil.getRequestUser().getUserName());
        return workingHoursService.update(form);
    }

    @Operation(summary = "验证 @author yjg")
    @GetMapping("/workingHours/queryByNumberAndDate/{jobNumber}/{overtimeData}")
    public ResponseDTO<String> queryByNumberAndDate(@PathVariable String jobNumber,@PathVariable String overtimeData) {
        return workingHoursService.queryByNumberAndDate(jobNumber,overtimeData);
    }

    @Operation(summary = "导出工时信息 ")
    @PostMapping("/workingHours/exportExcel")
    public void exportExcel(@RequestBody @Valid WorkingHoursQueryForm queryForm, HttpServletResponse response) throws IOException {
        if("1".equals(queryForm.getExcelFlag())){ //工时月结
            List<WorkingHoursMonthExcelVO> data = workingHoursService.getExcelMonthExportData(queryForm);
            if (CollectionUtils.isEmpty(data)) {
                SmartResponseUtil.write(response, ResponseDTO.userErrorParam("暂无数据"));
                return;
            }

            //String watermark = AdminRequestUtil.getRequestUser().getActualName();
           // watermark += SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMD_HMS);

            SmartExcelUtil.exportExcel(response,"工时月结信息.xlsx","月结日期"+queryForm.getStartTime()+"到"+queryForm.getEndTime(),WorkingHoursMonthExcelVO.class,data);
        }else{
            List<WorkingHoursExcelVO> data = workingHoursService.getExcelExportData(queryForm);
            if (CollectionUtils.isEmpty(data)) {
                SmartResponseUtil.write(response, ResponseDTO.userErrorParam("暂无数据"));
                return;
            }

            String watermark = AdminRequestUtil.getRequestUser().getActualName();
            watermark += SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMD_HMS);

            SmartExcelUtil.exportOverTimeExcel(response,"工时加班信息.xlsx","工时"+queryForm.getStartTime()+"到"+queryForm.getEndTime(),WorkingHoursExcelVO.class,data);
        }
    }

    @Operation(summary = "导出加班信息 ")
    @PostMapping("/workingOverTime/exportExcel")
    public void exportWorkingOverTimeExcel(@RequestBody @Valid WorkingHoursQueryForm queryForm, HttpServletResponse response) throws IOException {
        if("1".equals(queryForm.getExcelFlag())){ //工时月结
            List<WorkingHoursMonthExcelVO> data = workingHoursService.getExcelMonthExportData(queryForm);
            if (CollectionUtils.isEmpty(data)) {
                SmartResponseUtil.write(response, ResponseDTO.userErrorParam("暂无数据"));
                return;
            }

            String watermark = AdminRequestUtil.getRequestUser().getActualName();
            watermark += SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMD_HMS);

            SmartExcelUtil.exportExcel(response,"工时月结信息.xlsx","月结日期"+queryForm.getStartTime()+"到"+queryForm.getEndTime(),WorkingHoursMonthExcelVO.class,data);
        }else{
            List<WorkingHoursExcelVO> data = workingHoursService.getExcelExportData(queryForm);
            if (CollectionUtils.isEmpty(data)) {
                SmartResponseUtil.write(response, ResponseDTO.userErrorParam("暂无数据"));
                return;
            }

            String watermark = AdminRequestUtil.getRequestUser().getActualName();
            watermark += SmartLocalDateUtil.format(LocalDateTime.now(), SmartDateFormatterEnum.YMD_HMS);

            SmartExcelUtil.exportOverTimeExcel(response,"工时加班信息.xlsx","工时"+queryForm.getStartTime()+"到"+queryForm.getEndTime(),WorkingHoursExcelVO.class,data);
        }
    }



    @Operation(summary = "查询 @author yjg")
    @PostMapping("/workingHours/selectPdfDownData")
    @SaCheckPermission("workinghours:preview")
    public ResponseDTO<List<WorkingHoursPdfVO>> selectPdfDownData(@RequestBody @Valid WorkingHoursQueryForm queryForm) {
        queryForm.setCreateUserId(SmartRequestUtil.getRequestUserId());
        return ResponseDTO.ok(workingHoursService.selectPdfDownLoadData(queryForm));
    }

    @Operation(summary = "更新 @author yjg")
    @PostMapping("/workinghoursallow/update")
    public ResponseDTO<String> allowUpdate(@RequestBody @Valid WorkingHoursUpdateForm updateForm) {
        return workingHoursService.update(updateForm);
    }

    @Operation(summary = "加班审批编辑")
    @PostMapping("/workingovertimeallow/update")
    public ResponseDTO<String> workingOvertimeAllowUpdate(@RequestBody @Valid WorkingHoursUpdateForm updateForm) {
        ResponseDTO<String> update = workingHoursService.update(updateForm);
      //  WorkingOvertimeAnalysisForm workingHoursEntity = SmartBeanUtil.copy(updateForm, WorkingOvertimeAnalysisForm.class);
     //   workingHoursService.analysis(workingHoursEntity);
        return update;

    }

    @Operation(summary = "查询 @author yjg")
    @PostMapping("/workingHours/selectPdfOverTimeData")
  //  @SaCheckPermission("overtime:preview")
    public ResponseDTO<List<WorkingHoursPdfOverTimeVO>> selectPdfOverTimeData(@RequestBody @Valid WorkingHoursQueryForm queryForm) {
        queryForm.setCreateUserId(SmartRequestUtil.getRequestUserId());
        return ResponseDTO.ok(workingHoursService.selectPdfOverTimeData(queryForm));
    }

    @Operation(summary = "分析数据 @author yjg")
    @PostMapping("/workingHours/analysisData")
    public ResponseDTO<String> analysisData(@RequestBody @Valid WorkingOvertimeAnalysisForm queryForm){
        workingHoursService.analysis(queryForm);
        return ResponseDTO.ok();
    }

    @Operation(summary = "批量审批 @author yjg")
    @PostMapping("/workingHours/batchApprove")
    public ResponseDTO<String> batchApprove(@RequestBody ValidateList<Integer> idList) {
        return workingHoursService.batchApprove(idList);
    }
}
